//
//  MainViewController.swift
//  ydy_weibo
//
//  Created by 杨德勇 on 16/7/12.
//  Copyright © 2016年 杨德勇. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {
    //为了安全，需要用到private，但是因为btn是oc的，点击会崩溃，所以要把方法也变成oc的
   @objc private func composeBtnDidClick(){
    
    let composeVC = ComposeViewController()
    let nav = UINavigationController(rootViewController: composeVC)
    presentViewController(nav, animated: true, completion: nil)
    
    
    }


    override func viewDidLoad() {
        super.viewDidLoad()
        
        //由于tabbar只读，不能直接操作，所以修改的话要用kvc
        let mainTabbar = MainTabBar()
        //kvc赋值
        setValue(mainTabbar, forKey: "tabBar")
        
        addChildViewControllers()
        
        //为中间的按钮添加点击事件
        mainTabbar.composeBtn.addTarget(self, action: #selector(self.composeBtnDidClick), forControlEvents: UIControlEvents.TouchUpInside)

       
    }
    

    private func addChildViewControllers() {
        addChildViewController(HomeTableViewController(), title: "首页", imgName: "tabbar_home")
        addChildViewController(MessageTableViewController(), title: "消息", imgName: "tabbar_message_center")
        addChildViewController(DiscoverTableViewController(), title: "发现", imgName: "tabbar_discover")
        addChildViewController(ProfileTableViewController(), title: "我", imgName: "tabbar_profile")
            }
    
    
    
    private func addChildViewController(vc:UIViewController,title:String,imgName:String) {
       
//        self.tabBar.tintColor = UIColor.orangeColor()
        let nav = UINavigationController(rootViewController: vc)
        
        vc.title = title
        vc.tabBarItem.image = UIImage(named: imgName)
        vc.tabBarItem.selectedImage = UIImage(named: imgName + "_highlighted")?.imageWithRenderingMode(.AlwaysOriginal)
//    用tabbar的tintcolor替代这种方法     vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName :UIColor .orangeColor()], forState: UIControlState.Selected)
        
        
        addChildViewController(nav)
        
        

    }

}
